package com.lld.crowdfunding.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lld.crowdfunding.entity.QuanXian;
import com.lld.crowdfunding.entity.User;
import com.lld.crowdfunding.mapper.UserMapper;
import com.lld.crowdfunding.service.UserService;
import com.lld.crowdfunding.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
<<<<<<< HEAD
=======
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
>>>>>>> 42dbb3a (20210806)
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
<<<<<<< HEAD
=======
import java.util.Date;
>>>>>>> 42dbb3a (20210806)
import java.util.List;

@Transactional
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;


    /*实验方法*/
    @Override
    public Msg getAll() {
        List<User> all = userMapper.getAll();
        return Msg.success().add("all", all);
    }

    /*查询所有用户并分页，通过用户名(loginName),角色(role),*/
    public Msg getAllUser(String loginName, Integer role, Integer pn, Integer pageSize) {

        Page<User> page = PageHelper.startPage(pn, pageSize);

        //查询
        List<User> allUser = userMapper.getAllUser(loginName, role);

        PageInfo<User> info = new PageInfo<>(allUser,5);

        return Msg.success().add("pageInfo", info);

    }

    /*修改用户*/
    public Msg updateUser(User user,MultipartFile file) throws IOException {

        User oldUser = userMapper.getUserById(user.getId());
        File oldFile = new File("G://images//"+oldUser.getPicPath());
        String newFileName = user.getLoginName()+file.getOriginalFilename();
        if(!file.getOriginalFilename().equals("")) {
            if(oldFile.mkdir()) {
                oldFile.delete();
            }
            user.setPicPath(user.getLoginName()+file.getOriginalFilename());
            file.transferTo(new File("G://images//",newFileName));
        }

        int i = userMapper.updateUser(user);
        if (i > 0) {
            return Msg.success();
        }
        return Msg.error();
    }


    /*根据id查询用户*/
    public Msg getUserById(Long id) {
        User user = userMapper.getUserById(id);
        return Msg.success().add("user", user);
    }

    /*新增注册(文件上传)*/
    @Override
    public Msg saveUser(User user, MultipartFile file) throws IOException {
        File path = new File("G://images");
        if(!path.mkdir()) {
            path.mkdir();
        }
        if(!file.isEmpty()) {
            user.setPicPath(user.getLoginName()+file.getOriginalFilename());
            file.transferTo(new File(path,"//"+user.getLoginName()+file.getOriginalFilename()));
        }
<<<<<<< HEAD
=======
        user.setCreatetime(new Date());
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(user.getUserPassword());
        user.setUserPassword(encode);
>>>>>>> 42dbb3a (20210806)
        int i = userMapper.saveUser(user);
        if (i > 0) {
            return Msg.success();
        }
        return Msg.error();
    }

    /*删除用户*/
    @Override
    public Msg deleteUser(Long id) {
        int i = userMapper.deleteUser(id);
        if (i > 0) {
            return Msg.success();
        }
        return Msg.error();
    }

    /*逻辑删除*/
    @Override
    public Msg unableUser(Long id, Integer isDeleted) {
        int i = userMapper.unableUser(id, isDeleted);
        if (i > 0) {
            return Msg.success();
        }
        return Msg.error();
    }

    /*登录*/
    @Override
    public Msg login(String loginName, String userPwd, HttpSession session,
                     HttpServletRequest request, HttpServletResponse response) {
        User u = userMapper.login(loginName);
<<<<<<< HEAD
        if (u == null) {
            return Msg.error().add("fail", "该用户不存在");
        } else if (!u.getUserPassword().equals(userPwd)) {
=======
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        if (u == null) {
            return Msg.error().add("fail", "该用户不存在");
        } else if (!bCryptPasswordEncoder.matches(userPwd, u.getUserPassword())) {
>>>>>>> 42dbb3a (20210806)
            return Msg.error().add("fail", "用户名密码错误");
        } else {
            List<QuanXian> userQuanxian = userMapper.getUserQuanxian(u.getId());
            session.setAttribute("quanxian",userQuanxian);
            session.setAttribute("user", u);
            Cookie userCookie = new Cookie("cookie_username",u.getLoginName());//服务器创建cookie,存储内容为用户名
            userCookie.setMaxAge(60*60*24);//设置cookie存在时间为一天
            userCookie.setPath("/");//当前项目都携带这个cookie
            response.addCookie(userCookie);//向客户端发送cookie
            return Msg.success();
        }
    }

    /*主界面*/
    @Override
    public Msg main(HttpSession session) {
        List<QuanXian> list = (List<QuanXian>) session.getAttribute("quanxian");
<<<<<<< HEAD
        QuanXian quanXian = list.get(0);
        return Msg.success().add("mainPage",quanXian.getQxUrl());
=======
        if(list.size()>0) {
            QuanXian quanXian = list.get(0);
            return Msg.success().add("mainPage",quanXian.getQxUrl());
        }
        return Msg.success().add("mainPage","noAuthority");
>>>>>>> 42dbb3a (20210806)
    }
}
